package org.example.common.api;

import lombok.AllArgsConstructor;
import lombok.Data;

/**
 * Api返回状态码接口
 * Created by Mr.Huang on 2024/11/19
 */
@Data
@AllArgsConstructor
public class Response<T> {

    /**
     * 返回数据
     */
    private T data;
    /**
     * 返回信息
     */
    private String message;
    /**
     * 返回状态码
     */
    private int code;

    /**
     * 返回成功
     */
    public static <K> Response<K> success() {
        return new Response<>(null, ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getCode());
    }

    /**
     * 返回成功
     *
     * @param data 获取的数据
     */
    public static <K> Response<K> success(K data) {
        return new Response<>(data, ResultCode.SUCCESS.getMessage(), ResultCode.SUCCESS.getCode());
    }

    /**
     * 返回成功
     *
     * @param message 提示信息
     */
    public static <K> Response<K> success(String message) {
        return new Response<>(null, message, ResultCode.SUCCESS.getCode());
    }

    /**
     * 返回服务器错误
     */
    public static <K> Response<K> failed() {
        return new Response<>(null, ResultCode.FAILED.getMessage(), ResultCode.FAILED.getCode());
    }

    /**
     * 返回服务器错误
     *
     * @param code 错误码
     * @param message 提示信息
     */
    public static <K> Response<K> failed(int code, String message) {
        return new Response<>(null, message, code);
    }

    /**
     * 返回服务器错误
     *
     * @param resultCode 错误的枚举码
     */
    public static <K> Response<K> failed(IResultCode resultCode) {
        return new Response<>(null, resultCode.getMessage(), resultCode.getCode());
    }

    /**
     * 返回接口请求失败
     */
    public static <K> Response<K> apiFailed() {
        return new Response<>(null, ResultCode.API_FAILED.getMessage(), ResultCode.API_FAILED.getCode());
    }

    /**
     * 返回接口请求失败
     *
     * @param message 提示信息
     */
    public static <K> Response<K> apiFailed(String message) {
        return new Response<>(null, message, ResultCode.API_FAILED.getCode());
    }

    /**
     * 返回未登录结果
     */
    public static <K> Response<K> unauthorized() {
        return new Response<>(null, ResultCode.UNAUTHORIZED.getMessage(), ResultCode.UNAUTHORIZED.getCode());
    }

    /**
     * 返回未权限结果
     */
    public static <K> Response<K> forbidden() {
        return new Response<>(null, ResultCode.FORBIDDEN.getMessage(), ResultCode.FORBIDDEN.getCode());
    }
}
